package chapter08_class

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test05_Inherit
 * Package: chapter08_class
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-07-03
 *          Time: 9:43
 */


//继承和多态 总结来说 就是先父后子 先主后辅
object Test05_Inherit {
  def main(args: Array[String]): Unit = {
    val stu1 = new Student7("alice",19)
    stu1.info
    println("-----------------------------------")
    val stu2 = new Student7("bob",20,209000653)
    stu2.info

    val t = new Person5("父类",123)

    val teacher = new Teacher
    teacher.info

    //动态绑定 运行绑定 根据对象实例来判断
    def info(person: Person5): Unit ={
      person.info
    }
    //把一个子类的对象实例传给一个 父类 或者是接口的引用 可以实现同一个接口 不同实现

    //多态 动态绑定 Scala中熟悉和方法都是动态绑定 Java中只有方法是动态绑定
    println("----------------")
    info(stu1)
    info(stu2)
    info(t)
    info(teacher)


  }
}

//定义父类
class Person5(){
  var name:String = _
  var age:Int = _
  println("1.父类主构造器")

  //创建辅助构造器
  def this(name:String,age:Int){
    this() //直接调用主构造器
    println("2.父类的辅助构造器调用")
    this.name = name
    this.age = age
  }

  def info: Unit ={
    println(s"Person: name = $name , age = $age")
  }
}

//定义子类 子类参数列表不需要写val var 因为是继承父类的 后面这个参数列表就会对应的去找父类存在对应的主或者辅构造器
//继承父类的参数列表也可以不用写
class Student7(name:String,age:Int) extends Person5(name,age){
  var stuNo:Int = _
  println("3.子类的主构造器")

  def this(name:String,age:Int,stuNo:Int){
    this(name,age)
    println("4.子类的辅助构造器")
    this.stuNo = stuNo
  }

  //重写父类的info
  override def info: Unit = {
    println(s"Student name: $name , age: $age , stuNo: $stuNo")
  }

}


//在创建一个子类 验证多态
class Teacher extends Person5{
  override def info: Unit = {
    println("teach")
  }

}